package com.itcast.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itcast.constant.MessageConstant;
import com.itcast.entity.PageResult;
import com.itcast.entity.QueryPageBean;
import com.itcast.entity.Result;
import com.itcast.pojo.Role;
import com.itcast.service.RoleService;
import com.itcast.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {


    @Reference
    private UserService userService;

    @Reference
    private RoleService roleService;

//    @Autowired
//    private BCryptPasswordEncoder bCryptPasswordEncoder;
    /**
     * 查询用户名
     */
    @RequestMapping("/getUserName")
    public String getUserName(){
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (user!=null){
            String username = user.getUsername();
            return username;
        }
        return null;
    }

    /**
     * 查询用户菜单
     * @param username
     * @return
     */
    @RequestMapping("/getMenu")
    public Result getMenu(String username){
        try {
            List<Map<String, Object>> data = userService.getMenu(username);

            return new Result(true,"查询菜单列表成功",data);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询菜单列表失败");
        }
    }

    /**
     * 分页查询
     * @param map
     * @return
     */
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody Map map){
        PageResult pageResult = userService.findPage(map);
        return pageResult;
    }

    /**
     * 添加用户
     * @param user
     * @return
     */
    @RequestMapping("/add")
    public Result add (@RequestBody com.itcast.pojo.User user,Integer[] roleIds){
        try {
            Result result = userService.add(user,roleIds);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"用户添加失败");
        }
    }
    @RequestMapping("/getRoles")
    public List<Role> getRoles(){
        List<Role> roleList = roleService.findAll();
        return roleList;
    }

    /**
     * 获取用户拥有的角色
     * @param userId
     * @return
     */
    @RequestMapping("/getRoleIds")
    public Result getRoleIds(Integer userId){
        List<Integer> roleIds = roleService.findRoleIdsByUserId(userId);
        return new Result(false,"查询权限关联",roleIds);
    }

    /**
     * 回显编辑用户信息
     * @param userId
     * @return
     */
    @RequestMapping("/getUser")
    public Result getUser(Integer userId){
        com.itcast.pojo.User user = userService.findUserById(userId);
        return new Result(false,"查询用户信息成功",user);
    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody com.itcast.pojo.User user,Integer[] roleIds){
        try {
            userService.edit(user,roleIds);
            return new Result(true,"用户修改成功");
        } catch (Exception e) {
            e.printStackTrace();
        return new Result(false,"用户修改失败");
        }
    }
    @RequestMapping("/delUser")
    public Result delUser(int id){
        try {
            userService.delUser(id);
            return new Result(true,"用户删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"用户删除失败");
        }
    }

    @RequestMapping("/delAll")
    public Result delAll(@RequestBody List<com.itcast.pojo.User> list){
        try {
            userService.delAllUser(list);
            return new Result(true,"用户删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"用户删除失败");
        }
    }


}
